package com.rljk.domain.po;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rljk.common.consts.AssetConst;
import com.rljk.config.property.TencentProperty;
import com.rljk.utils.security.Base64Utils;
import com.rljk.utils.security.Sha256Utils;
import com.tencent.trustsql.sdk.Constants;
import com.tencent.trustsql.sdk.TrustSDK;
import com.tencent.trustsql.sdk.util.HttpClientUtil;
import com.tencent.trustsql.sdk.util.SignStrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;

import java.util.*;

/**
 * Author:   YASUO
 * Date:     2018/7/25 15:40
 * Description:资产发行提交
 */
@Slf4j
public class AssetIssueSubmit {

    public String execute(String prvKey,SortedMap<String, Object> paramMap) throws Exception {

        log.info("提交资产");

//        Map<String,Object> map = new HashMap<>();
//        map.put("account","12P5GfV3DaWVuNdato1ZG37YmbA3egHmR4");          //签名账户地址
//        map.put("sign_str","d4e3085b038f6a2b9adbc0bd091b6ad586208a44d6fb3b222464efe7f0c0c91f");//待签名串
//        map.put("sign",TrustSDK.SignRenString(AssetConst.PUBLISH_PRIVATE_KEY,
//                Hex.decodeHex("d4e3085b038f6a2b9adbc0bd091b6ad586208a44d6fb3b222464efe7f0c0c91f".toCharArray())));         //使用发行方的私钥对待签名串进行签名,调用sdk的SignRenString函数



        ArrayList list = new ArrayList();
        Map<String,Object> map = new TreeMap<>();
        map.put("account","12P5GfV3DaWVuNdato1ZG37YmbA3egHmR4");          //签名账户地址
        map.put("id",1);
        map.put("sign_str","3c080f0ef73bbefead6f79d8b591c121504df6d05c0341b2f022dc8a521ae596");//待签名串
        map.put("sign",TrustSDK.SignRenString(AssetConst.PUBLISH_PRIVATE_KEY,
                Hex.decodeHex("3c080f0ef73bbefead6f79d8b591c121504df6d05c0341b2f022dc8a521ae596".toCharArray())));
        list.add(map);
        Object sign_list = JSONObject.toJSON(list);
        log.info("sign_list字符串："+sign_list);
        paramMap.put("sign_list", sign_list); //签名列表
        log.info("sign_list:"+sign_list.toString());

        JSONArray resultJsonArr = new JSONArray();
//        Map<String, Object> paramMap = new TreeMap<String, Object>();
        paramMap.put("version", AssetConst.version);
        paramMap.put("sign_type", AssetConst.sign_type);
        paramMap.put("mch_id", AssetConst.mch_id);
//        if (StringUtils.isNotEmpty(jsonObj.getString("node_id"))) {
//            paramMap.put("node_id", jsonObj.getString("node_id"));
//        }
        paramMap.put("node_id", AssetConst.node_id);
        paramMap.put("chain_id", AssetConst.chain_id);
        paramMap.put("ledger_id", AssetConst.ledger_id);
        //paramMap.put("owner_account", AssetConst.owner_account);
        paramMap.put("asset_type", AssetConst.asset_type);


        paramMap.put("transaction_id","201808201450269234");//交易ID
        paramMap.put("asset_id","26aENhEn4tW5nR8mqTsnwLcKEgsravTLARXD3BG8pzfEjaG");//资产ID
        paramMap.put("timestamp", System.currentTimeMillis() / 1000);       //unix时间戳
        paramMap.put("mch_sign", TrustSDK.signString(prvKey, SignStrUtil.mapToKeyValueStr(paramMap).getBytes(), false));

        log.info("拼接参数："+SignStrUtil.mapToKeyValueStr(paramMap));
        // generate post data
        JSONObject postJson = new JSONObject();
        for (String key : paramMap.keySet()) {
            postJson.put(key, paramMap.get(key));
        }

        log.info("postJson:"+postJson.toJSONString());

        String url = AssetConst.asset_issue_submit;
        String result = HttpClientUtil.post(url, postJson.toJSONString());
        // 分析http请求结果
        JSONObject resultJson = JSONObject.parseObject(result);

        log.info("分析http请求结果resultJson:"+resultJson);
        if ("0".equals(resultJson.getString("retcode")) && "OK".equals(resultJson.getString("retmsg"))) {
            // 验证返回数据的mch_sign
            paramMap.clear();
            for (Map.Entry<String, Object> entry : resultJson.entrySet()) {
                if (!"mch_sign".equals(entry.getKey())) {
                    paramMap.put(entry.getKey(), entry.getValue());
                }
            }
            boolean mchSignValid = TrustSDK.verifyString(TencentProperty.get("COMMUNICATION_PUBLIC_KEY"),
                    SignStrUtil.mapToKeyValueStr(paramMap), resultJson.getString("mch_sign"));
            resultJson.put("mch_sign_verify", mchSignValid);
            log.info(" 验证返回数据的mch_sign:"+mchSignValid);
        }
        resultJsonArr.add(0, resultJson);
        return resultJsonArr.toJSONString();
    }

}
